package com.shutong.edu.invoke.retry.interceptor;

import com.shutong.edu.invoke.retry.model.RetryInfo;
import com.shutong.edu.invoke.retry.processor.AopRetryProcessor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
 *
 * @ClassName: RetryAopInterceptor.java
 * @author: Naughty Guo
 * @date: Jun 2, 2016
 */
public class RetryAopInterceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(RetryAopInterceptor.class);

    /**
     * intercept target method.
     *
     * @param proceedingJoinPoint
     * @return Object
     * @author Naughty Guo Jun 2, 2016
     */
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) {
        Object[] arguments = proceedingJoinPoint.getArgs();
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();
        RetryInfo retryInfo = new RetryInfo();
        retryInfo.setProceedingJoinPoint(proceedingJoinPoint);
        retryInfo.setTargetMethod(method);
        retryInfo.setTargetProxyObject(proceedingJoinPoint.getTarget());
        retryInfo.setTargetOriginalObject(proceedingJoinPoint.getTarget());
        retryInfo.setArguments(arguments);
        return new AopRetryProcessor().retryProcess(retryInfo);
    }
}